//
//  TIMSystemMessageCell.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/6/29.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMSystemMessageCell.h"
#import "NSDate+TPSChat.h"
#import "Masonry.h"
#import "TIMDemoMacro.h"

#import "TIMMessage.h"

#define     TIMELABEL_HEIGHT    20.0f
#define     TIMELABEL_SPACE_Y   10.0f
#define     AVATAR_SPACE_Y      12.0f
#define     MSG_SPACE_TOP       2
#define     MSG_SPACE_BTM       2
#define     MSG_SPACE_LEFT      5
#define     MSG_SPACE_RIGHT     5

@interface TIMSystemMessageCell ()

@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIImageView *messageBackgroundView;
@property (nonatomic, strong) TIMMessage *currentMes;


@end

@implementation TIMSystemMessageCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setBackgroundColor:[UIColor clearColor]];
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        [self.contentView addSubview:self.messageBackgroundView];
        [self.contentView addSubview:self.timeLabel];
        [self.contentView addSubview:self.messageLabel];
        [self p_addMasonry];
    }
    return self;
}

- (void)didTapConirm
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(messageCellTOCinform:)]) {
        [self.delegate messageCellTOCinform:_currentMes];
        _currentMes.clickState = TIMMessageDidClick;
    }
}

- (void)setMessage:(TIMMessage *)message
{
    _currentMes = message;
    if (message.operateGroupType == TIMOperateGroupTypeGrpOfferAuditCommit) {
        if (message.clickState == TIMMessageUnClick) {
            self.messageLabel.text = [NSString stringWithFormat:@"%@,去确定",message.text];
            UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapConirm)];
            [tapGR setNumberOfTapsRequired:2];
            [self addGestureRecognizer:tapGR];
        } else {
             self.messageLabel.text = [NSString stringWithFormat:@"%@,已确定",message.text];
        }
    } else {
        self.messageLabel.text = message.text;
    }
   
    // 时间
    [self.timeLabel setText:[NSString stringWithFormat:@"  %@  ", message.date.chatTimeInfo]];
    
    if (!_message || _message.showTime != message.showTime) {
        [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(message.showTime ? TIMELABEL_HEIGHT : 0);
            make.top.mas_equalTo(self.contentView).mas_offset(message.showTime ? TIMELABEL_SPACE_Y : 0);
        }];
    }
    [self.messageLabel setContentCompressionResistancePriority:500 forAxis:UILayoutConstraintAxisHorizontal];
    
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.messageBackgroundView).mas_offset(MSG_SPACE_LEFT);
        make.top.mas_equalTo(self.messageBackgroundView).mas_offset(MSG_SPACE_TOP);
    }];
    [self.messageBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.messageLabel).mas_offset(MSG_SPACE_RIGHT);
        make.bottom.mas_equalTo(self.messageLabel).mas_offset(MSG_SPACE_BTM);
    }];
    
    // 3.1 设置文本最大尺寸
    CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40 - 10 * 10, MAXFLOAT);
    // 3.2 计算文本真实尺寸
    CGSize textRealSize = [self sizeWithFont:[UIFont systemFontOfSize:15] maxSize:textMaxSize string:message.text];
    
    [self.messageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(textRealSize);
    }];
}
- (void)p_addMasonry
{
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).mas_offset(TIMELABEL_SPACE_Y);
        make.centerX.mas_equalTo(self.contentView);
    }];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeLabel.mas_bottom).mas_offset(AVATAR_SPACE_Y);
        make.centerX.mas_equalTo(self.contentView);
    }];
    
    [self.messageBackgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeLabel.mas_bottom).mas_offset(AVATAR_SPACE_Y);
        make.centerX.mas_equalTo(self.contentView);
    }];
}

/** 测量文本的尺寸 */
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize string:(NSString *)string {
    NSDictionary *attrs = @{NSFontAttributeName: font};
    CGSize size =  [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    CGSize realSize = CGSizeMake(size.width, size.height + 20);
    return realSize;
}

#pragma mark - Getter -
- (UILabel *)timeLabel
{
    if (_timeLabel == nil) {
        _timeLabel = [[UILabel alloc] init];
        [_timeLabel setFont:TPSFontSize(11)];
        [_timeLabel setTextColor:UIColor.whiteColor];
        _timeLabel.backgroundColor = RGBColor(211, 211, 211);
        _timeLabel.layer.cornerRadius = 5.0;
        _timeLabel.layer.masksToBounds = YES;
    }
    return _timeLabel;
}
- (UILabel *)messageLabel
{
    if (_messageLabel == nil) {
        _messageLabel = [[UILabel alloc] init];
        [_messageLabel setFont:TPSFontSize(13)];
        [_messageLabel setNumberOfLines:0];
        [_messageLabel setTextColor:[UIColor blackColor]];
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _messageLabel;
}
- (UIImageView *)messageBackgroundView
{
    if (_messageBackgroundView == nil) {
        _messageBackgroundView = [[UIImageView alloc] init];
        _messageBackgroundView.layer.cornerRadius = 4.0;
        _messageBackgroundView.layer.masksToBounds = YES;
        _messageBackgroundView.backgroundColor = UIColor.whiteColor;
    }
    return _messageBackgroundView;
}
@end
